package com.interagriculture.controller;

import com.interagriculture.annotation.AdminVerify;
import com.interagriculture.common.Result;
import com.interagriculture.entity.UserInfo;
import com.interagriculture.service.UserInfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * @author 10 2024/10/24
 */
@RestController
@RequestMapping("/adminUserInfo")
public class AdminUserInfoController {

     @Resource
     private UserInfoService userInfoService;
     /**
      * 添加或更新成员信息
      */
     @PostMapping("/saveOrUpdateInfo")
//     @AdminVerify
     public Result saveOrUpdateInfo(Integer id,
                                    Integer studentId,
                                    String name,
                                    String specialty,
                                    String email,
                                    String qq,
                                    String direction,
                                    String blog,
                                    String briInfo,
                                    @RequestParam(required = false) MultipartFile picture) throws IOException {

          UserInfo userInfo = new UserInfo();
          userInfo.setId(id);
          userInfo.setStudentId(studentId);
          userInfo.setName(name);
          userInfo.setSpecialty(specialty);
          userInfo.setEmail(email);
          userInfo.setQq(qq);
          userInfo.setDirection(direction);
          userInfo.setBlog(blog);
          userInfo.setBriInfo(briInfo);
          userInfoService.saveOrUpdateInfo(userInfo,picture);
          return Result.ok();
     }

     @GetMapping("/{id}")
     @AdminVerify
     public Result<UserInfo> getById(@PathVariable @NotNull Integer id){
          return Result.ok(userInfoService.selectById(id));
     }

     @PostMapping("/delete")
     @AdminVerify
     public Result deleteById(@RequestParam @NotNull Integer id){
          userInfoService.deleteById(id);
          return Result.ok("删除成功");
     }
}
